<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="MQTT消息ID" prop="msgid">
        <el-input v-model="queryParams.msgid" placeholder="请输入MQTT消息ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="MQTT主题" prop="topic">
        <el-input v-model="queryParams.topic" placeholder="请输入MQTT主题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="MQTT消息的QoS" prop="qos">
        <el-input v-model="queryParams.qos" placeholder="请输入MQTT消息的QoS" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="数据插入时间" prop="insTim">
        <el-date-picker clearable v-model="queryParams.insTim" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择数据插入时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="消息来源Client ID" prop="clientId">
        <el-input v-model="queryParams.clientId" placeholder="请输入消息来源Client ID" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="消息来源用户名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入消息来源用户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户端的IPAddress" prop="peerhost">
        <el-input v-model="queryParams.peerhost" placeholder="请输入客户端的IPAddress" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="MQTT消息的Flags" prop="flags">
        <el-input v-model="queryParams.flags" placeholder="请输入MQTT消息的Flags" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="事件触发所在节点" prop="node">
        <el-input v-model="queryParams.node" placeholder="请输入事件触发所在节点" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="具体事件" prop="event">
        <el-input v-model="queryParams.event" placeholder="请输入具体事件" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-show="isAllAddButtonShow" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:msg:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-show="isAllModifyButtonShow" type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate" v-hasPermi="['system:msg:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-show="isAllDeleteButtonShow" type="danger" plain icon="el-icon-delete" size="mini"
          :disabled="multiple" @click="handleDelete" v-hasPermi="['system:msg:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:msg:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="msgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="MQTT消息ID" align="center" prop="msgid" />
      <el-table-column label="MQTT主题" align="center" prop="topic" />
      <el-table-column label="MQTT消息的QoS" align="center" prop="qos" />
      <el-table-column label="MQTT消息体" align="center" prop="payload">
        <template slot-scope="scope">
          <el-popover title="详细" trigger="hover" placement="top" width="400">
            <pre style="white-space: pre-wrap;">{{ scope.row.payload }}</pre>
            <div slot="reference">
              <p size="medium" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{ scope.row.payload }}
              </p>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="数据插入时间" align="center" prop="insTim" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.insTim, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息来源Client ID" align="center" prop="clientId" />
      <el-table-column label="消息来源用户名" align="center" prop="username" />
      <el-table-column label="客户端的IPAddress" align="center" prop="peerhost" />
      <el-table-column label="MQTT消息的Flags" align="center" prop="flags">
        <template slot-scope="scope">
          <el-popover title="详细" trigger="hover" placement="top" width="400">
            <pre style="white-space: pre-wrap;">{{ scope.row.flags }}</pre>
            <div slot="reference">
              <p size="medium" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">{{ scope.row.flags }}
              </p>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="事件触发所在节点" align="center" prop="node" />
      <el-table-column label="具体事件" align="center" prop="event" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-show="isAllModifyButtonShow" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['system:msg:edit']">修改</el-button>
          <el-button v-show="isAllDeleteButtonShow" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)" v-hasPermi="['system:msg:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleInfo(scope.row)"
            v-hasPermi="['system:msg:remove']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="20%">
        <el-form-item label="MQTT消息ID" prop="msgid">
          <el-input :readonly="true" v-model="form.msgid" placeholder="请输入MQTT消息ID" />
        </el-form-item>
        <el-form-item label="MQTT主题" prop="topic">
          <el-input :readonly="true" v-model="form.topic" placeholder="请输入MQTT主题" />
        </el-form-item>
        <el-form-item label="MQTT消息的QoS" prop="qos">
          <el-input :readonly="true" v-model="form.qos" placeholder="请输入MQTT消息的QoS" />
        </el-form-item>
        <el-form-item label="MQTT消息体" prop="payload">
          <el-input :readonly="true" type="textarea" :rows="7" :value="form.payload" />
        </el-form-item>
        <el-form-item label="数据插入时间" prop="insTim">
          <el-date-picker :readonly="true" clearable v-model="form.insTim" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择数据插入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消息来源Client ID" prop="clientId">
          <el-input :readonly="true" v-model="form.clientId" placeholder="请输入消息来源Client ID" />
        </el-form-item>
        <el-form-item label="消息来源用户名" prop="username">
          <el-input :readonly="true" v-model="form.username" placeholder="请输入消息来源用户名" />
        </el-form-item>
        <el-form-item label="客户端的IPAddress" prop="peerhost">
          <el-input :readonly="true" v-model="form.peerhost" placeholder="请输入客户端的IPAddress" />
        </el-form-item>
        <el-form-item label="MQTT消息的Flags" prop="flags">
          <el-input :readonly="true" v-model="form.flags" placeholder="请输入MQTT消息的Flags" />
        </el-form-item>
        <el-form-item label="事件触发所在节点" prop="node">
          <el-input :readonly="true" v-model="form.node" placeholder="请输入事件触发所在节点" />
        </el-form-item>
        <el-form-item label="具体事件" prop="event">
          <el-input :readonly="true" v-model="form.event" placeholder="请输入具体事件" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeForm">确 定</el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMsg, getMsg, delMsg, addMsg, updateMsg } from "@/api/tugboat/msg";

export default {
  name: "Msg",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      msgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        msgid: null,
        topic: null,
        qos: null,
        payload: null,
        insTim: null,
        clientId: null,
        username: null,
        peerhost: null,
        flags: null,
        node: null,
        event: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 是否显示所有修改按钮
      isAllModifyButtonShow: false,
      // 是否显示所有删除按钮
      isAllDeleteButtonShow: false,
      // 是否显示所有新增按钮
      isAllAddButtonShow: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listMsg(this.queryParams).then(response => {
        this.msgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        msgid: null,
        topic: null,
        qos: null,
        payload: null,
        insTim: null,
        clientId: null,
        username: null,
        peerhost: null,
        flags: null,
        node: null,
        event: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.msgid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const msgid = row.msgid || this.ids
      getMsg(msgid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.msgid != null) {
            updateMsg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMsg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    closeForm() {
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const msgids = row.msgid || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + msgids + '"的数据项？').then(function () {
        return delMsg(msgids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const msgid = row.msgid || this.ids
      getMsg(msgid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "详情";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/msg/export', {
        ...this.queryParams
      }, `msg_${new Date().getTime()}.xlsx`)
    },
    showTips(obj, row) {
      /*obj为鼠标移入时的事件对象*/

      /*currentWidth 为文本在页面中所占的宽度，创建标签，加入到页面，获取currentWidth ,最后在移除*/
      let TemporaryTag = document.createElement('span');
      TemporaryTag.innerText = row.payload;
      TemporaryTag.className = 'getTextWidth';
      document.querySelector('body').appendChild(TemporaryTag);
      let currentWidth = document.querySelector('.getTextWidth').offsetWidth;
      document.querySelector('.getTextWidth').remove();

      /*cellWidth为表格容器的宽度*/
      const cellWidth = obj.target.offsetWidth

      /*当文本宽度小于||等于容器宽度两倍时，代表文本显示未超过两行*/
      currentWidth <= (2 * cellWidth) ? row.showTooltip = false : row.showTooltip = true
      console.log(currentWidth)
      console.log(cellWidth)
      console.log(currentWidth <= (2 * cellWidth))
      console.log('row.showTooltip', row.showTooltip)
    }
  }
};
</script>
